package com.cqeec.waimaidemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.waimaidemo.common.R;
import com.cqeec.waimaidemo.dto.DishDto;
import com.cqeec.waimaidemo.entity.Category;
import com.cqeec.waimaidemo.entity.Dish;
import com.cqeec.waimaidemo.entity.Employee;
import com.cqeec.waimaidemo.service.CategoryService;
import com.cqeec.waimaidemo.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavors(dishDto);
        return R.success("新增成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> queryWrapper =new LambdaQueryWrapper<>();
//        添加条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        dishService.page(pageInfo,queryWrapper);
        Page<DishDto> dtoPage=new Page<>();
//        对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
//        获取dish 分页查询中的数据
        List<Dish> records=pageInfo.getRecords();
//        通过dish 表中的category_id这个字段 查询category_name
//        并且赋值给相应的dishDto 实体类中的categoryName字段
        List<DishDto> list=records.stream().map(item->{
            DishDto dishDto=new DishDto();
//            item 和dishDto就有相同字段了
            BeanUtils.copyProperties(item,dishDto);
//            获取dish表中的category_id值
            Long categoryId= item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if (byId!=null){
//                将category这张表查询的结果中的name赋值给categoryName
                dishDto.setCategoryName(byId.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
//    修改数据回显
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable("id")Long id){
//        dish id 与dish_flavors dish_id 连接查询
        DishDto dishDto= dishService.getByIdFlavors(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavors(dishDto);
        return R.success("修改成功");
    }
    @GetMapping("/list")
    public R<List<Dish>> getdish(Dish dish){
        LambdaQueryWrapper<Dish>  wrapper   =new LambdaQueryWrapper<>();
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        wrapper.eq(Dish::getStatus,1);
        wrapper.orderByAsc(Dish::getSort);
        List<Dish> list= dishService.list(wrapper);
        return R.success(list);
    }
}
